from fastapi import APIRouter, Depends
from app.core.responses import Response
from app.models import PointsRecord
from app.schemas.user_points import Points, Record
from app.services.auth import get_current_user
from app.services.user_points import UserPointsService

router = APIRouter()


@router.get('/points')
def index(page: int = 1, pageSize: int = 10, user: str = Depends(get_current_user)):
    return UserPointsService.index(page, pageSize)


@router.post('/points')
def store(form: Points, user: str = Depends(get_current_user)):
    if UserPointsService.create(form):
        return Response.message('操作成功')


@router.delete('/points/{id}')
def destroy(id: int, user: str = Depends(get_current_user)):
    if UserPointsService.delete(id):
        return Response.message('操作成功')


@router.post('/points/{id}')
def update(id: int, form: Points, user: str = Depends(get_current_user)):
    if UserPointsService.update(id, form):
        return Response.message('操作成功')


@router.post('/points_record')
def store_record(form: Record, user: str = Depends(get_current_user)):
    if UserPointsService.create_record(user.id, form):
        return Response.message('操作成功')
    return Response.failed('操作失败')


@router.delete('/points_record/{id}')
def destroy(id: int, user: str = Depends(get_current_user)):
    if UserPointsService.delete_record(id):
        return Response.message('操作成功')
